package yang.learn.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yang.learn.entity.ChatRoom;
import yang.learn.entity.ChatRoomFriend;
import yang.learn.entity.Contacts;
import yang.learn.entity.User;
import yang.learn.service.ChatRoomFriendService;
import yang.learn.service.ChatRoomService;
import yang.learn.service.ContactsService;
import yang.learn.service.UserService;
import yang.learn.service.impl.ContactsServiceImpl;
import yang.learn.service.impl.UserServiceImpl;

import java.util.List;

@Controller
public class MainController {

    @Autowired(required = false)
    private UserService userService;
    @Autowired(required = false)
    private ContactsService contactsService;
    @Autowired(required = false)
    private ChatRoomService chatRoomService;
    @Autowired(required = false)
    private ChatRoomFriendService chatRoomFriendService;

    @ResponseBody
    @RequestMapping("/")
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        System.out.println("用户登录状态："+request.getAttribute("login_result"));
        if ("success".equals(request.getAttribute("login_result"))) {
            //获取session中的用户id
            HttpSession session = request.getSession();
            User user = new User(Integer.parseInt(session.getAttribute("user_id").toString()));
            //获取用户信息
            user = userService.queryId(user);
            modelAndView.addObject(user);
            List<Contacts> contactsList = contactsService.queryAll(user);
            //初始化用户列表
            modelAndView.addObject("contactsList", contactsList);
            System.out.println("初始化首页信息");
            System.out.println("用户信息："+user);
            System.out.println("联系人信息："+contactsList);
            //初始化用户创建的聊天室信息
            List<ChatRoom> chatRoomList = chatRoomService.queryAll(user);
            List<ChatRoomFriend> chatRoomFriendList = chatRoomFriendService.queryAccount(user);
            //初始化用户加入的聊天室信息
            for (ChatRoomFriend chatRoomFriend : chatRoomFriendList) {
                ChatRoom chatRoom = new ChatRoom();
                chatRoom.setChat_id(chatRoomFriend.getChat_id());
                ChatRoom chatRoom1 = chatRoomService.queryId(chatRoom);
                chatRoomList.add(chatRoom1);
            }
            modelAndView.addObject(chatRoomList);
        }else if ("admin_success".equals(request.getAttribute("login_result"))) {
            modelAndView.setViewName("redirect:/");
        }else{
            User user = new User();
            user.setUser_name("未登录(点击登录)");
            modelAndView.addObject("context","hello.html");
            modelAndView.addObject("user", user);
        }
        return modelAndView;
    }

}